package com.generated;

import java.sql.*;
import java.util.ArrayList;
import java.util.List;

/**
 * 数据库链接
 */
public class BaseDao {

	//数据库连接参数
	private static final String URL ="jdbc:mysql://55064ad6.all123.net:3306";
	private static final String NAME = "root";
	private static final String PASS = "root";
	private static final String DRIVER ="com.mysql.jdbc.Driver";
	private static final String SOURCE = "information_schema";//数据库名
	static{
		try {
			Class.forName(DRIVER);
		} catch (ClassNotFoundException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public Connection getCon() throws SQLException{
		Connection con=DriverManager.getConnection(URL,NAME,PASS);
		return con;
	}
	public void setParams(PreparedStatement ps,Object[] params) throws SQLException{
		if (params!=null) {
			for (int i = 0; i < params.length; i++) {
				ps.setObject(i+1, params[i]);
			}
		}
	}
	
	public List<List> query(){
		Connection con = null;
		PreparedStatement ps = null;
		ResultSet rs = null;
		List<List> list = null;
		try {
			con = getCon();
			ps = con.prepareStatement("SELECT TABLE_SCHEMA,TABLE_NAME,COLUMN_NAME,DATA_TYPE,COLUMN_COMMENT FROM information_schema.`COLUMNS` WHERE TABLE_SCHEMA = ?");
			setParams(ps, new Object[]{SOURCE});
			rs=ps.executeQuery();
			int count=rs.getMetaData().getColumnCount();
			list = new ArrayList();
			while(rs.next()){
				List row=new ArrayList();
				for(int i=1;i<=count;i++){
					row.add(rs.getString(i));
					
				}
				list.add(row);
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		finally{
			closeAll(rs, ps, con);
		}
		return list;
	}
	public void closeAll(ResultSet rs,Statement st,Connection con){
		try {
			if (rs!=null) {
				rs.close();
				rs=null;
			}
			if (st!=null) {
				st.close();
				st=null;
			}
			if (con!=null) {
				con.close();
				con=null;
			}
		} catch (SQLException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
}
